package com.breezelin.freshmarket.activities;

/**
 * Created by Breeze Lin
 * 2016/9/21 14:37
 * breezesummerlin@gmail.com
 */

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.app.BaseActivity;
import com.breezelin.freshmarket.app.FMApplication;
import com.breezelin.freshmarket.entities.JsonStruct;
import com.breezelin.freshmarket.entities.data.UserInfo;
import com.breezelin.freshmarket.entities.network.ReqNRsp;
import com.breezelin.freshmarket.network.TaskCanceler;
import com.breezelin.freshmarket.network.asynctasks.RegisterTask;
import com.breezelin.freshmarket.network.asynctasks.VerifyCodeTask;
import com.breezelin.freshmarket.utils.LocalDataUtil;
import com.breezelin.freshmarket.utils.UserUtil;

/**
 * 注册页面
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

	/**
	 * 登陆
	 */
	public static final int CODE_LOGIN = 0x33;

	/**
	 * 电话号码输入框
	 */
	private EditText phoneEdit;
	/**
	 * 验证码输入框
	 */
	private EditText verifyCodeEdit;
	/**
	 * 密码输入框
	 */
	private EditText passwordEdit;
	/**
	 * 验证码发送按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private Button verifyCodeBtn;
	/**
	 * 注册按钮
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private Button registerBtn;
	/**
	 * 密码
	 */
	private String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		bindViews();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TaskCanceler.cancelRegisterRequests();
	}

	/**
	 * 绑定视图
	 */
	private void bindViews() {
		phoneEdit = (EditText) findViewById(R.id.edit_phone_no);
		verifyCodeEdit = (EditText) findViewById(R.id.edit_verify_code);
		passwordEdit = (EditText) findViewById(R.id.edit_password);
		verifyCodeBtn = (Button) findViewById(R.id.btn_send_verify_code);
		registerBtn = (Button) findViewById(R.id.btn_register);

		verifyCodeBtn.setOnClickListener(this);
		registerBtn.setOnClickListener(this);
	}

	/**
	 * 请求发送验证码
	 */
	private void sendVerifyCode() {
		new VerifyCodeTask(phoneEdit.getText().toString()) {
			@Override
			protected void onSuccess(ReqNRsp<JsonStruct> jsonStructReqNRsp) {
				if (jsonStructReqNRsp.getHeader().getRspCode() == 1) {
					showToast(R.string.toast_verify_required);
				} else {
					showToast(R.string.offline_or_svr_error);
				}
			}

			@Override
			protected void onError() {
				super.onError();
			}
		}.execute();
	}

	/**
	 * 发送注册请求
	 */
	private void register() {
		String phoneNo = phoneEdit.getText().toString();
		String verifyCode = verifyCodeEdit.getText().toString();
		password = passwordEdit.getText().toString();
		// 检查输入内容是否合法
		// todo 更精确地检查输入的内容
		if (!(TextUtils.isEmpty(phoneNo)
				|| TextUtils.isEmpty(verifyCode)
				|| TextUtils.isEmpty(password))) {
			new RegisterTask(phoneNo, verifyCode, password) {
				@Override
				protected void onSuccess(ReqNRsp<UserInfo> userInfoReqNRsp) {
					if (userInfoReqNRsp.getHeader().getRspCode() == 1) {
						showToast(R.string.toast_register_success);
						// 处理用户信息
						UserInfo userInfo = userInfoReqNRsp.getBody();
						UserUtil.saveUserData(userInfo);
						// 保存密码
						UserUtil.savePassword(password);
						// 保存登录状态
						FMApplication.isLogin = true;
						// 保存token
						LocalDataUtil.getInstance().put(
								LocalDataUtil.KEY_SVR_TOKEN,
								userInfo.getToken()
						);
						// 设置结果
						setResult(RESULT_OK);
						finish();
					}
				}

				@Override
				protected void onError() {
					super.onError();
					showToast(R.string.offline_or_svr_error);
				}
			}.execute();
		} else {
			FMApplication.isLogin = false;
			showToast(R.string.toast_check_input);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			// 验证码按钮
			case R.id.btn_send_verify_code:
				sendVerifyCode();
				break;
			// 注册按钮
			case R.id.btn_register:
				register();
				break;
		}
	}
}
